package exo1.fileCounter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.WeakHashMap;

public class FileWordsCount {

	public FileWordsCount(String filePath){
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filePath));
			StringBuilder fileData = new StringBuilder();

			char[] buf = new char[1024];
			int numRead=0;
			while((numRead=reader.read(buf)) != -1){
			fileData.append(buf, 0, numRead);
			}
			reader.close();
			
			StringTokenizer tockenizer=new StringTokenizer(fileData.toString());
			WeakHashMap<String, Integer> hashMap = new WeakHashMap<String, Integer>();
			while (tockenizer.hasMoreElements()) {
				String key=(String) tockenizer.nextElement();
				if(hashMap.get(key)==null)
					hashMap.put(key,new Integer(0));
				hashMap.put(key,Integer.valueOf(hashMap.get(key).intValue() + 1));
			}
			for(Entry<String, Integer> entry : hashMap.entrySet()) {
			    String cle = entry.getKey();
			    Integer valeur = entry.getValue();
			    System.out.println(cle+":"+valeur);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		new FileWordsCount("zero.txt");
	}
}
